package com.qf.controller.caiwu_controller;

import com.github.pagehelper.PageInfo;
import com.qf.bean.AccountItem;
import com.qf.bean.Prefund;
import com.qf.service.caiwu_service.AccountItemService;
import com.qf.utils.caiwu.vo.PrefundReportVO;
import com.qf.utils.caiwu.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2019/12/11 0011
 * Author->DT
 * sassspringboot
 * 行到水穷处 坐看云起时
 * 模块：账目事项模块
 */
@Controller
@RequestMapping("/item")
public class AccountItemController {
    @Autowired
    private AccountItemService accountItemService;
    //跳转到事项添加页
    @RequestMapping("/add-ui")
    public String addUI(Model model){
        //查询出全部事项,放到请求域中
        List<AccountItem> itemList=accountItemService.selectAll();
        model.addAttribute("itemList", itemList);
        return "/caiwu/account-item-add";
    }

    //执行事项添加
    @RequestMapping("/add")
    @ResponseBody
    public ResultVO add(AccountItem accountItem){
        //执行添加
        int count = accountItemService.insert(accountItem);
        //返回
        if (count==1){
            return new ResultVO(0, "添加记账事项成功！", null);
        }
        return new ResultVO(1, "添加记账事项失败！", null);
    }

    //跳转记账事项设置
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")Integer index,
                       Model model,String content){
        //0.封装参数
        Map map=new HashMap();
        map.put("index", index);
        map.put("page", 3);
        map.put("content", content);
        //分页查询事项
        PageInfo<AccountItem> pageInfo = accountItemService.findItemListByLimit(map);
        //将查询到的信息放入Request域中
        model.addAttribute("pageInfo", pageInfo);
        //数据回显信息放入Request域中
        model.addAttribute("content", content);
        return "/caiwu/account-item-list";
    }

    //跳转到编辑页面
    @RequestMapping("/edit-ui")
    public String editUI(Model model,Integer id){
        //查询对应id事项
        AccountItem accountItem = accountItemService.selectByPrimaryKey(id);
        model.addAttribute("accountItem", accountItem);
        return "/caiwu/account-item-edit";
    }
    //编辑事项
    @RequestMapping("/edit")
    @ResponseBody
    public ResultVO edit(AccountItem accountItem){
        //执行编辑
        int count = accountItemService.updateByPrimaryKeySelective(accountItem);
        //返回
        if (count==1){
            return new ResultVO(0, "编辑记账事项成功！", null);
        }
        return new ResultVO(1, "编辑记账事项失败！", null);
    }

    //删除事项
    @RequestMapping("/delete")
    @ResponseBody
    public ResultVO delete(Integer id){
        //执行删除
        int count = accountItemService.deleteByPrimaryKey(id);
        //返回
        if (count==1){
            return new ResultVO(0, "删除记账事项成功！", null);
        }
        return new ResultVO(1, "删除记账事项失败！", null);
    }

}
